package com.card.aac.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.card.aac.dao.AacMapper;
import com.card.aac.domain.Aac;
import com.card.aac.domain.AacExample;
import com.card.aac.domain.AacExample.Criteria;
import com.card.aac.service.AacService;
import com.card.framework.mybatis.mapper.BatchCallBack;
import com.card.framework.mybatis.mapper.MyFrameworkSupperMapper;
import com.card.framework.mybatis.optimist.ModifiedByAnotherUserException;
import com.card.framework.page.model.PageModel;
@Service("AacServiceImpl")
public class AacServiceImpl extends MyFrameworkSupperMapper implements AacService{

	@Override
	public Map<String, Object> selectAacForPage(Aac aac, PageModel pageModel) {
		AacMapper mapper = this.getMapper(AacMapper.class);
		AacExample aacExample = new AacExample();
		//AacExample.createCriteria().andUsernameIsNotNull();
		//AacExample.createCriteria().andUsernameEqualTo(Aac.getUsername());
		aacExample.setPage(pageModel);
		if(StringUtils.isNotEmpty(pageModel.getSort())) {
			String sort = pageModel.getSort();
			if(sort.indexOf("shop") == -1){
				sort = "ac." + sort;
			}else{
				sort = "ab." + sort.substring(sort.indexOf(".") + 1);
			}
			aacExample.setOrderByClause(pageModel.getSort()+" "+pageModel.getOrder());
		}
//		if(StringUtils.isNotEmpty(aac.getUsername())) 
//		aacExample.createCriteria().andUsernameEqualTo(aac.getUsername());
//		if(aac.getCardkey() != null)
//			aacExample.createCriteria().andIdEqualTo(aac.getCardkey());
//		if(StringUtils.isNotEmpty(aac.getNamesimp()))
//			aacExample.createCriteria().andNamesimpLike("%" + aac.getNamesimp() + "%");
//		if(StringUtils.isNotEmpty(aac.getTelephone()))
//			aacExample.createCriteria().andTelephoneLike("%" + aac.getTelephone() + "%");
		
		Criteria criteria = aacExample.createCriteria();
		criteria.andDelflgEqualTo((byte)1);
		if(aac.getCardkey() != null)
			criteria.andCardkeyEqualTo(aac.getCardkey());
		if(aac.getShop() != null && StringUtils.isNotEmpty(aac.getShop().getNamesimp()))
			criteria.andShopNamesimpLike("%" + aac.getShop().getNamesimp() + "%");
		if(aac.getCreatdate() != null)
			criteria.andCreatdateEqualTo(aac.getCreatdate());
		List<Aac> list = mapper.selectByExample(aacExample);
		Map<String, Object> modelMap = new HashMap<String, Object>();  
		modelMap.put("total", pageModel.getTotalCount());  
		modelMap.put("rows", list); 
		return modelMap;
	}

	@Override
	public void deleteAacs(final List<Aac> aac)
			throws ModifiedByAnotherUserException, Exception {
		batchSubmit(AacMapper.class,new BatchCallBack() {
			@Override
			public <E> void batchCallBack(E mapper) {
				if(aac != null && aac.size() > 0) {
					for(Aac a :aac){
						a.setDelflg((byte)0);
						((AacMapper) mapper).updateByPrimaryKeySelective(a);
					}
				}
			}
		});
	}
	@Override
	@Transactional
	public void addOrUpdate(Aac aac) {
		try{
			AacMapper mapper = this.getMapper(AacMapper.class);
			System.out.println("aac.getCardkey()="+aac.getCardkey());
			//Aaa queryResult = mapper.selectByPrimaryKey(aaa.getUserid());
			if(aac.getVersion()==null){
				mapper.insert(aac);
			}else
			{
				AacExample aacExample = new AacExample();
				aacExample.createCriteria().andCardkeyEqualTo(aac.getCardkey());
				mapper.updateByExample(aac, aacExample);
			}
		}catch(Exception e){
			e.printStackTrace();
		}
	}

	@Override
	public Aac getById(long id) {
		AacMapper mapper = this.getMapper(AacMapper.class);
	    return mapper.selectByPrimaryKey(id);
	}
	public static void main(String[] args){
		String a = "shop.id";
		System.out.println(a.substring(a.indexOf(".") +1));
	}
}
